package day06;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

@WebServlet("/download")
public class FileDownloadServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("FileDownloadServlet.service()");

        /*
            attachment:以附件方式打开
            inline:在线打开
         */
        response.setHeader("content-disposition",
                           new String("attachment;filename=啊.zip".getBytes(StandardCharsets.UTF_8),"ISO8859-1"));

        System.out.println("扣积分");


        ServletContext application=getServletContext();


        FileInputStream in=null;
        OutputStream out=null;

        try{
            in=new FileInputStream(application.getRealPath("/WEB-INF/file/ooad.zip"));
            out=response.getOutputStream();

            byte[] b=new byte[20480];

            int n;

            while((n=in.read(b))!=-1){
                out.write(b,0,n);
            }



        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(in!=null){
                in.close();
            }
        }






    }
}
